﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Medival_Snake
{
    public class Hero
    {
        public bool MoveUp = false;
        public bool MoveDown = false;
        public bool MoveLeft = false;
        public bool MoveRight = false;

        Vector2 TempLoc = Vector2.Zero;

        public Vector2 Position;
        public Texture2D Texture;
        public Rectangle Sensor;
        public Vector2 Move = new Vector2(2,2);
        Vector2 BaseMove = new Vector2(2, 2);
        public bool SensorCheck = true;

        public Hero(Vector2 pos, Texture2D tex)
        {
            this.Position = pos;
            this.Texture = tex;
        }

        public Rectangle Getbounds()
        {
            return new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
        }

        public void ResetMove() { Move = BaseMove; }

        public void Update()
        {
            Sensor = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);

            KeyboardState ks = Keyboard.GetState();

            //keybinds
            if (ks.IsKeyDown(Keys.A))
                MoveLeft = true;
            else
                MoveLeft = false;

            if(ks.IsKeyDown(Keys.D))
                MoveRight = true;
            else
                MoveRight = false;

            if(ks.IsKeyDown(Keys.S))
                MoveDown = true;
            else
                MoveDown = false;
            
            if(ks.IsKeyDown(Keys.W))
                MoveUp = true;
            else
                MoveUp = false;

            //keybind Efffects
            if (MoveDown)
                Sensor.Y += (int)Move.Y;
            if (MoveUp)
                Sensor.Y += (int)-Move.Y;
            if (MoveRight)
                Sensor.X += (int)Move.X;
            if (MoveLeft)
                Sensor.X += (int)-Move.X;

            TempLoc.X = Sensor.X;
            TempLoc.Y = Sensor.Y;

            for (int i = 0; i < Game1.Lvl1.Solids.Count; i++)
            {
                if (Sensor.Intersects(Game1.Lvl1.Solids[i]))
                {
                    SensorCheck = false;
                    break;
                }

            }

            if (SensorCheck)
            {
                Position.X = TempLoc.X;
                Position.Y = TempLoc.Y;
            }
            SensorCheck = true;

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Getbounds(), Color.White);
        }
    }
}
